#coding=utf-8

from views.view_base import ViewBase
from controller.common_controller import CommonController
from common.middlewares import *
from conf.constant import *
from flask import make_response


# 字典类
class CommonView(ViewBase):
    @dispatch('/words', methods=['GET', 'POST'])
    def words(self):
        code = RESPONSE_FAILED
        data = {}
        msg = ''

        try:
            controller = CommonController()

            data['words'] = controller.get_words()
            code = RESPONSE_SUCCESS
            msg = 'success'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data=data, msg=msg)

    @dispatch('/log', methods=['GET', 'POST'])
    def log(self):
        # code = RESPONSE_FAILED
        # data = {}
        # msg = ''
        resp = ''
        try:
            line = (self.params.get('line')) or '200'
            time_str = self.params.get('time')
            keyword = self.params.get('keyword')

            controller = CommonController()
            data = controller.get_log(time_str, line, keyword)
            # code = RESPONSE_SUCCESS
            # msg = 'success'
            resp = make_response(data)
            resp.headers["Content-type"] = "text/plan;charset=UTF-8"

        except:
            writelog(traceback.format_exc())
        return resp

    @dispatch('/error_log', methods=['GET', 'POST'])
    def error_log(self):
        # code = RESPONSE_FAILED
        # data = {}
        # msg = ''
        resp = ''
        try:
            controller = CommonController()
            data = controller.get_error_log()
            # code = RESPONSE_SUCCESS
            # msg = 'success'
            resp = make_response(data)
            resp.headers["Content-type"] = "text/plan;charset=UTF-8"

        except:
            writelog(traceback.format_exc())

        return resp

    @dispatch('/read_obj', methods=['GET', 'POST'])
    def read_obj(self):
        code = RESPONSE_FAILED
        data = {}
        msg = ''
        try:
            name = self.params.get('name')
            controller = CommonController()
            data = controller.read_obj(name)
            code = RESPONSE_SUCCESS
            msg = 'success'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data=data, msg=msg)



